package com.yiyibook.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yiyibook.entity.BookBigStyle;
import com.yiyibook.mapper.BookBigStyleMapper;
import com.yiyibook.service.BookBigStyleService;
import com.yiyibook.util.Constants;
import com.yiyibook.util.ResultEntity;
import com.yiyibook.util.SetPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 〈功能简述〉
 *
 * @author xun.gong@hand-china.com
 * @date 2019/10/12
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BookBigStyleServiceImpl implements BookBigStyleService {

    @Autowired
    private BookBigStyleMapper bookBigStyleMapper;


    @Override
    public ResultEntity<BookBigStyle> selectAllByCondition(PageInfo pageInfo, Map<String, String> conditionMap) {
        // 保存查询结果以及分页信息
        ResultEntity<BookBigStyle> resultEntity = new ResultEntity<>();
        // 当前页
        int pageNum = 1;
        // 页面大小
        int pageSize = 5;
        // 判断是否有分页信息
        Integer[] pageMsg = SetPageInfo.setValue(pageInfo, pageNum, pageSize);
        // 查询条件
        BookBigStyle bookBigStyle = new BookBigStyle();
        if(conditionMap != null){
            Set<String> keySet = conditionMap.keySet();
            for(String key : keySet){
                if(Constants.Condition.NAME.equals(key)){
                    bookBigStyle.setBigStyleName(conditionMap.get(key));
                }
                resultEntity.setSearchTitle(key);
                resultEntity.setKeyWord(conditionMap.get(key));
            }
        }
        // 分页插件
        PageHelper.startPage(pageMsg[0],pageMsg[1]);
        // 查询所有的图书大类信息 取 第 pageNum 页 ，页面大小为 pageSize
        List<BookBigStyle> bookBigStyles = bookBigStyleMapper.selectAllByCondition(bookBigStyle);
        // 获取新的分页信息
        PageInfo<BookBigStyle> pageInfoNew = new PageInfo<>(bookBigStyles);
        resultEntity.setEntityList(bookBigStyles);
        resultEntity.setPageInfo(pageInfoNew);
        return resultEntity;
    }

    @Override
    public int insertSelective(BookBigStyle record) {
        return bookBigStyleMapper.insertSelective(record);
    }

    @Override
    public BookBigStyle selectOneByCondition(BookBigStyle record) {
        List<BookBigStyle> bookBigStyles = bookBigStyleMapper.selectAllByCondition(record);
        if(CollectionUtils.isNotEmpty(bookBigStyles)){
            return bookBigStyles.get(0);
        }else{
            return null;
        }
    }

    @Override
    public int updateByPrimaryKeySelective(BookBigStyle record) {
        return bookBigStyleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer bigStyleId) {
        return bookBigStyleMapper.deleteByPrimaryKey(bigStyleId);
    }

    @Override
    public int deleteAnyBigStyleByIds(List<Integer> bigStyleIds) {
        return bookBigStyleMapper.deleteAnyBigStyleByIds(bigStyleIds);
    }
}
